package com.songlanyun.modules.config.service.impl;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.config.dao.ProfitSharingDao;
import com.songlanyun.modules.config.entity.ProfitSharingEntity;
import com.songlanyun.modules.config.model.ProfitSharingDTO;
import com.songlanyun.modules.config.service.ProfitSharingService;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分润配置
 *
 * @author lijiahe
 * @email
 * @date 2022-09-21 14:58:22
 */
@Service("profitSharingService")
public class ProfitSharingServiceImpl extends ServiceImpl<ProfitSharingDao, ProfitSharingEntity>
    implements ProfitSharingService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProfitSharingEntity> page =
            this.page(new Query<ProfitSharingEntity>().getPage(params), new LambdaQueryWrapper<ProfitSharingEntity>());
        return new PageUtils(page);
    }

    @Override
    public ProfitSharingEntity info() {

        return baseMapper.selectOne(new LambdaQueryWrapper<ProfitSharingEntity>().last("limit 1"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateProfitSharing(ProfitSharingDTO dto) {
        if (ObjectUtil.isNull(dto)) {
            return;
        }
        ProfitSharingEntity info = this.info();
        ProfitSharingEntity entity = new ProfitSharingEntity();
        BeanUtil.copyPropertiesIgnoreNull(dto, entity);
        if (ObjectUtil.isNull(info)) {
            baseMapper.insert(entity);
        } else {
            entity.setId(info.getId());
            baseMapper.updateById(entity);
        }
    }

}
